// pages/teamdetail/teamdetail.js
var app = getApp()
var num = 0
var num2 = 0
var range, token = ''
var goodstimer
var selectattrvalue = ''
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
var sceneres = {}
var winScale = 750 / app.data.deviceInfo.windowWidth
Page({

  /**
   * 页面的初始数据
   */
  data: {
    reminder: [{
      'img': 'https://img-oss.shihanbainian.com/upload_dev/images/b/b/0/bb0ce0e18e3a1b632e5b63c63345d282.png',
      'word': '你来慢了，拼团人数已满'
    }, {
      'img': 'https://img-oss.shihanbainian.com/upload_dev/images/1/0/9/109874b8b461f07f9fb7b9b91966815a.png',
      'word': '拼团失败，该团未在规定时间内成团'
    }, {
      'img': 'https://img-oss.shihanbainian.com/upload_dev/images/6/f/4/6f4146baa73b13c1d5b20ee760eea45f.png',
      'word': '拼团已结束'
    }],
    selectnum: 1,
    dialogflag: true,
    groupstatus: '', //0-你来慢了，拼团人数已满;1-拼团失败，该团未在规定时间内成团;2-拼团已结束;
    showLoading: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    var that = this
    var res = wx.getSystemInfoSync()
    this.setData({
      winWidth: res.windowWidth
    })
    range = 794 / (750 / this.data.winWidth)
    var animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease-in-out',
    });
    this.animation = animation;
    if (options.scene) {
      var scene1 = decodeURIComponent(options.scene).split('&'); //tid=2&pid=123
      if (scene1 && scene1.length > 0) {
        for (var i = 0; i < scene1.length; i++) {
          var str = scene1[i].split('=')
          if (str[0] == 'tid') {
            this.setData({
              teamid: str[1]
            })
          } else if (str[0] == 'pid') {
            sceneres['partner_id'] = str[1]
          } else if (str[0] == 'cid') {
            sceneres['cid'] = str[1]
          }
        }
      }
    } else {
      this.setData({
        teamid: options.teamid || ''
      })
      if (options.pid) {
        sceneres['partner_id'] = options.pid
      }
    }
    console.log(sceneres)
    app.wechatSwitch().then(function(wechat_switch) { //是否打开微信授权开关
      app.commonLogin(sceneres, wechat_switch)
    })
  },
  // 拼团详情
  teamdetail: function() { //product_team_view  product_team
    var that = this
    shihan.sendRequest(config.product_team_view + that.data.teamid + '.html', 'GET', {})
      .then(function(response) {
        if (response.data.code == 0) {
          console.log(response)
          if (response.data.data.sh_error) {
            wx.showModal({
              title: "提示",
              content: response.data.data.sh_error_content,
              confirmText: "确认",
              confirmColor: "#c40000",
              showCancel: false,
              success: function(res) {
                if (res.confirm) {
                  wx.switchTab({
                    url: '/pages/home/home',
                  })
                }
              }
            })
          } else {
            var teamimglist = [];
            if (response.data.data.sh_product.sh_number > 0) {
              for (var i = 0; i < response.data.data.sh_product.sh_number; i++) {
                teamimglist[i] = {}
                teamimglist[i].imgbg = 'https://img-oss.shihanbainian.com/upload_dev/images/e/8/8/e881261c91322d65250e81ec8c8c62b7.png';
                if (response.data.data.sh_customer[i] && response.data.data.sh_customer[i].sh_nick_name) {
                  teamimglist[i].sh_nick_name = response.data.data.sh_customer[i].sh_nick_name;
                  teamimglist[i].sh_headimgurl = response.data.data.sh_customer[i].sh_headimgurl;
                }
              }
            }
            that.setData({
              teamimglist: teamimglist,
              goodsid: response.data.data.sh_product.sh_id,
              teamdetail: response.data.data
            })
            // console.log(that.data)
            clearInterval(goodstimer);
            goodstimer = setInterval(function() {
              var newgoodstime = {}
              newgoodstime['sh_end_time'] = response.data.data.sh_end_at
              that.timeCalculation(newgoodstime)
            }, 50)
            that.goodsdetail();
          }
        } else {
          wx.showModal({
            title: "提示",
            content: response.data.msg,
            confirmText: "确认",
            confirmColor: "#c40000",
            showCancel: false,
            success: function(res) {
              if (res.confirm) {
                wx.switchTab({
                  url: '/pages/home/home',
                })
              }
            }
          })
        }
      }, function(error) {
        console.log(error);
      });
  },
  // 商品详情
  goodsdetail: function() {
    var that = this
    shihan.sendRequest(config.goods_detail + that.data.goodsid + '.html', 'GET', {})
      .then(function(response) {
        if (response.data.code == 0) {
          wx.stopPullDownRefresh()
          wx.reportAnalytics('page_product_detail', {
            product_id: that.data.goodsid,
            product_name: response.data.data.sh_name
          })
          var bigarr = response.data.data.sh_attributes
          for (var i = 0; i < bigarr.length; i++) {
            for (var j = 0; j < bigarr[i].sh_options.length; j++) {
              bigarr[i].selectattrid = ''
              bigarr[i].sh_options[j].active = false
              bigarr[i].sh_options[j].num = 1
            }
          }
          that.setData({
            showLoading: false,
            goodsinfo: response.data.data
          })
          that.setdisabled()
        }
      }, function(error) {
        console.log(error);
      });
  },
  // 拼团时间计算
  timeCalculation: function(staretime) { //sh_create_at:"2017-08-28 16:23:26" 秒杀时间计算
    var that = this
    var time1 = new Date().getTime(), //取得当前毫秒数
      time2 = new Date(staretime.sh_end_time.replace(/-/g, '/')); //取得指定时间的总毫秒数 

    if (time2 - time1 > 0) { //正在进行中
      var micro_second = time2 - time1;

      var time = micro_second / 1000;
      // 小时位
      var hour = Math.floor(time / 3600);
      hour = hour <= 9 ? '0' + hour : hour;
      // 分钟位
      var minute = Math.floor(time % 3600 / 60);
      minute = minute <= 9 ? '0' + minute : minute;
      // 秒数
      var second = Math.floor(time % 3600 % 60);
      second = second <= 9 ? '0' + second : second;
      //毫秒
      var millisecond = Math.floor((micro_second % 1000) / 100);

      that.setData({
        changetime: {
          status: true,
          changeHour: hour,
          changeMinute: minute,
          changeSecond: second + '.' + millisecond
        }
      })
    } else {
      that.setData({
        changetime: {
          status: false
        }
      })
    }
    if (that.data.teamdetail.sh_customer.length == that.data.teamdetail.sh_product.sh_number) {
      that.setData({
        groupstatus: 0 //拼团失败，该团未在规定时间内成团;
      })
    }
    if (that.data.changetime && !that.data.changetime.status && (that.data.teamdetail.sh_customer.length < that.data.teamdetail.sh_product.sh_number)) {
      that.setData({
        groupstatus: 1 //拼团失败，该团未在规定时间内成团;
      })
    }
    if (that.data.changetime && that.data.changetime.status && (that.data.teamdetail.sh_customer.length == 0)) {
      that.setData({
        groupstatus: 2 //拼团已结束;
      })
    }
  },
  viewgoodsDetail: function(e) {
    wx.navigateTo({
      url: '/pages/goodsdetail/goodsdetail?goodsid=' + e.currentTarget.dataset.id
    })
  },
  grouprule: function(e) {
    var url = e.currentTarget.dataset.url;
    if (url&&url.indexOf('https://') == 0) {
      wx.navigateTo({
        url: '/pages/strategyinfo/strategyinfo?share=0&nologin=1&url=' + encodeURIComponent(url)
      })
    }
  },
  opengroupbuy: function() {
    wx.navigateTo({
      url: '/pages/other/groupbuy/groupbuy'
    })
  },
  //打开选择SKU弹窗
  openskuselect: function(e) {
    token = wx.getStorageSync('token');
    if (token) {
      if (this.data.teamdetail.sh_poor_join_num > 0) {
        var typebuy = e.currentTarget.dataset.typebuy
        this.setData({
          poptype: e.currentTarget.dataset.type
        })
        if (typebuy == 'indirectbuy') {
          this.setData({
            buymode: typebuy,
            typebuyword: '加入购物车',
            buyment: 'indirectbuy'
          })
        } else if (typebuy == 'direcbuy' || typebuy == 'groupbuy' || typebuy == "originbuy" || typebuy == 'secondbuy') {
          this.setData({
            buymode: typebuy,
            buyteamid: e.currentTarget.dataset.teamid || '',
            typebuyword: '立即购买',
            buyment: 'direcbuy'
          })
        }
        this.checkselectsku();
        this.animation.translateY(-range).step();
        this.setData({
          showSkuStatus: true,
          drawerscroll: 0,
          animationData: this.animation.export(),
          detailpage: false
        });
      } else {
        shihan.showToast.call(this, this.data.teamdetail.success_lable);
        this.teamdetail();
      }
    } else {
      app.commonNoLogin()
    }
  },
  //关闭选择SKU弹窗
  closeskuselect: function(e) {
    var that = this, drawerflag = true, bigarr = this.data.goodsinfo.sh_attributes;

    if (e.currentTarget.dataset.operate == 'joincart') {
      for (var i = 0; i < bigarr.length; i++) {
        if (bigarr[i].selectattrid == '') {
          drawerflag = false;
          shihan.showToast.call(this, '请选择' + bigarr[i].sh_label);
          break
        }
      }
      if (drawerflag) {
        if (this.data.buyment == 'direcbuy') {
          var buydata = {}
          buydata["buy_product"] = JSON.parse('{"' + that.data.goodsinfo.selectattrallid + '"' + ':' + that.data.selectnum + '}')
          if (e.currentTarget.dataset.buymode == 'groupbuy') {
            buydata["is_group_buy"] = '1'
            buydata["team_id"] = that.data.buyteamid
          }
          shihan.sendRequest(config.confirm_order, 'POST', buydata)
            .then(function(response) {
              if (response.data.code == 0) {
                that.animation.translateY(range).step();
                that.setData({
                  showSkuStatus: false,
                  animationData: that.animation.export(),
                  detailpage: true
                });
                if (e.currentTarget.dataset.buymode == 'groupbuy') {
                  wx.navigateTo({
                    url: '/pages/firmorder/firmorder?product_id=' + that.data.goodsinfo.selectattrallid + '&number=' + that.data.selectnum + '&buytype=directbuy&groupbuy=1&originbuy=0&teamid=' + that.data.buyteamid,
                  })
                } else if (e.currentTarget.dataset.buymode == 'originbuy') {
                  wx.navigateTo({
                    url: '/pages/firmorder/firmorder?product_id=' + that.data.goodsinfo.selectattrallid + '&number=' + that.data.selectnum + '&buytype=directbuy&groupbuy=0&originbuy=1',
                  })
                } else {
                  wx.navigateTo({
                    url: '/pages/firmorder/firmorder?product_id=' + that.data.goodsinfo.selectattrallid + '&number=' + that.data.selectnum + '&buytype=directbuy&groupbuy=0&originbuy=0',
                  })
                }
              } else if (response.data.code == 1101) { //未登录
                app.commonNoLogin()
              } else {
                shihan.showToast.call(that, response.data.msg);
                if (response.data.code == 1112) {
                  that.animation.translateY(range).step();
                  that.setData({
                    showSkuStatus: false,
                    animationData: that.animation.export(),
                    detailpage: true
                  });
                }
              }
            }, function(error) {
              console.log(error);
            });
        }
      }
    } else {
      this.animation.translateY(range).step();
      this.setData({
        showSkuStatus: false,
        animationData: this.animation.export(),
        detailpage: true
      });
    }
  },
  selectsku: function(e) {
    var that = this
    var big = e.currentTarget.dataset.a
    var small = e.currentTarget.dataset.b
    var bigarr = this.data.goodsinfo.sh_attributes
    var activeitem = this.data.goodsinfo.sh_attributes[big]
    var numflag = false
    //点击时加上选中效果，并且判断该属性库存是否大于0 
    if (e.currentTarget.dataset.num > 0 && bigarr.length > 1) {
      // 如果属性没有选中
      if (activeitem.sh_options[small].active == false) {
        for (var i = 0; i < bigarr[big].sh_options.length; i++) {
          activeitem.sh_options[i].active = false
        }
        activeitem.sh_options[small].active = true
        bigarr[big].selectattr = activeitem.sh_options[small].sh_alias_name
        bigarr[big].selectattrid = activeitem.sh_options[small].sh_option_id

        if (big == 0) {
          for (var l = 0; l < bigarr[1].sh_options.length; l++) {
            var codeago = bigarr[big].selectattrid + '_' + bigarr[1].sh_options[l].sh_option_id
            for (var m = 0; m < this.data.goodsinfo.sh_childs.length; m++) {
              var codelast = this.data.goodsinfo.sh_childs[m].sh_sku_code;
              if (codeago == codelast) {
                if (that.data.buymode == "originbuy") {
                  bigarr[1].sh_options[l].num = this.data.goodsinfo.sh_childs[m].sh_no_promotion_stock;
                } else {
                  bigarr[1].sh_options[l].num = this.data.goodsinfo.sh_childs[m].sh_stock;
                }
              }
            }
          }
          for (var a = 0; a < bigarr.length; a++) {
            for (var b = 0; b < bigarr[a].sh_options.length; b++) {
              if (bigarr[a].sh_options[b].active == true) {
                numflag = true
                break
              }
            }
          }
          if (!numflag) {
            for (var a = 0; a < bigarr.length; a++) {
              for (var b = 0; b < bigarr[a].sh_options.length; b++) {
                bigarr[a].sh_options[b].num = 1
              }
            }
          }
          this.setData({
            thumbnail: activeitem.sh_options[small].sh_image || '',
            selectimgindex: small
          })
        } else if (big == 1) {
          for (var n = 0; n < bigarr[0].sh_options.length; n++) {
            var codeago = bigarr[0].sh_options[n].sh_option_id + '_' + bigarr[big].selectattrid
            for (var o = 0; o < this.data.goodsinfo.sh_childs.length; o++) {
              var codelast = this.data.goodsinfo.sh_childs[o].sh_sku_code;
              if (codeago == codelast) {
                if (that.data.buymode == "originbuy") {
                  bigarr[0].sh_options[n].num = this.data.goodsinfo.sh_childs[o].sh_no_promotion_stock;
                } else {
                  bigarr[0].sh_options[n].num = this.data.goodsinfo.sh_childs[o].sh_stock;
                }
              }
            }
          }
          for (var a = 0; a < bigarr.length; a++) {
            for (var b = 0; b < bigarr[a].sh_options.length; b++) {
              if (bigarr[a].sh_options[b].active == true) {
                numflag = true
                break
              }
            }
          }
          if (!numflag) {
            for (var a = 0; a < bigarr.length; a++) {
              for (var b = 0; b < bigarr[a].sh_options.length; b++) {
                bigarr[a].sh_options[b].num = 1
              }
            }
          }
        }
      } else if (activeitem.sh_options[small].active == true) { //如果属性选中
        activeitem.sh_options[small].active = false
        bigarr[big].selectattr = ''
        bigarr[big].selectattrid = ''
        this.setData({
          priceflag: true
        })
        if (that.data.buymode == "originbuy") {
          this.data.selectattrmoney = this.data.goodsinfo.sh_no_promotion_show_price
        } else {
          this.data.selectattrmoney = this.data.goodsinfo.sh_show_price
        }
        if (big == 0) {
          for (var b = 0; b < bigarr[0].sh_options.length; b++) {
            if (bigarr[0].sh_options[b].active == true) {
              numflag = true
              break
            }
          }
          if (!numflag) {
            for (var b = 0; b < bigarr[1].sh_options.length; b++) {
              bigarr[1].sh_options[b].num = 1
            }
            if (that.data.buymode == "originbuy") {
              this.data.goodsinfo.selectattrstock = this.data.goodsinfo.sh_no_promotion_all_stock
              this.data.goodsinfo.selectattrcommission = this.data.goodsinfo.sh_max_no_promotion_partner_commission
            } else {
              this.data.goodsinfo.selectattrstock = this.data.goodsinfo.sh_all_stock
              this.data.goodsinfo.selectattrcommission = this.data.goodsinfo.sh_max_partner_commission
            }
          }
        } else if (big == 1) {
          for (var b = 0; b < bigarr[1].sh_options.length; b++) {
            if (bigarr[1].sh_options[b].active == true) {
              numflag = true
              break
            }
          }
          if (!numflag) {
            for (var b = 0; b < bigarr[0].sh_options.length; b++) {
              bigarr[0].sh_options[b].num = 1
            }
            if (that.data.buymode == "originbuy") {
              this.data.goodsinfo.selectattrstock = this.data.goodsinfo.sh_no_promotion_all_stock
              this.data.goodsinfo.selectattrcommission = this.data.goodsinfo.sh_max_no_promotion_partner_commission
            } else {
              this.data.goodsinfo.selectattrstock = this.data.goodsinfo.sh_all_stock
              this.data.goodsinfo.selectattrcommission = this.data.goodsinfo.sh_max_partner_commission
            }
          }
        }

      }
    } else if (e.currentTarget.dataset.num > 0 && bigarr.length == 1 && e.currentTarget.dataset.skustock > 0) {
      if (activeitem.sh_options[small].active == false) {
        for (var i = 0; i < bigarr[big].sh_options.length; i++) {
          activeitem.sh_options[i].active = false
        }
        activeitem.sh_options[small].active = true
        bigarr[big].selectattr = activeitem.sh_options[small].sh_alias_name
        bigarr[big].selectattrid = activeitem.sh_options[small].sh_option_id
        this.setData({
          thumbnail: activeitem.sh_options[small].sh_image || '',
          selectimgindex: small
        })
      } else if (activeitem.sh_options[small].active == true) { //如果属性选中
        activeitem.sh_options[small].active = false
        bigarr[big].selectattr = ''
        bigarr[big].selectattrid = ''
        this.setData({
          priceflag: true
        })
        if (that.data.buymode == "originbuy") {
          this.data.selectattrmoney = this.data.goodsinfo.sh_no_promotion_show_price
        } else {
          this.data.selectattrmoney = this.data.goodsinfo.sh_show_price
        }
      }
    }

    if (bigarr.length > 1) {
      for (var k = 0; k < this.data.goodsinfo.sh_childs.length; k++) {
        var code = this.data.goodsinfo.sh_childs[k].sh_sku_code
        if (code == bigarr[0].selectattrid + '_' + bigarr[1].selectattrid) {
          if (this.data.buymode == "originbuy") {
            this.data.goodsinfo.selectattrstock = this.data.goodsinfo.sh_childs[k].sh_no_promotion_stock;
            this.data.selectattrmoney = this.data.goodsinfo.sh_childs[k].sh_no_promotion_show_price;
            this.data.goodsinfo.selectattrcommission = this.data.goodsinfo.sh_childs[k].sh_no_promotion_partner_commission;
          } else {
            this.data.goodsinfo.selectattrstock = this.data.goodsinfo.sh_childs[k].sh_stock;
            this.data.selectattrmoney = this.data.goodsinfo.sh_childs[k].sh_show_price
            this.data.goodsinfo.selectattrcommission = this.data.goodsinfo.sh_childs[k].sh_partner_commission;
          }
          this.data.goodsinfo.selectattrallid = this.data.goodsinfo.sh_childs[k].sh_id
          this.setData({
            priceflag: false
          })
        }
      }
      if (this.data.goodsinfo.selectattrstock > 0) {
        if (this.data.selectnum > this.data.goodsinfo.selectattrstock) {
          this.setData({
            selectnum: 1,
            minutesimg: ''
          })
        }
        this.setData({
          minutesimg: '',
          plusimg: '3'
        })
      }
    } else {
      for (var k = 0; k < this.data.goodsinfo.sh_childs.length; k++) {
        var code = this.data.goodsinfo.sh_childs[k].sh_sku_code
        if (code == bigarr[0].selectattrid) {
          if (this.data.buymode == "originbuy") {
            this.data.goodsinfo.selectattrstock = this.data.goodsinfo.sh_childs[k].sh_no_promotion_stock;
            this.data.selectattrmoney = this.data.goodsinfo.sh_childs[k].sh_no_promotion_show_price;
            this.data.goodsinfo.selectattrcommission = this.data.goodsinfo.sh_childs[k].sh_no_promotion_partner_commission;
          } else {
            this.data.goodsinfo.selectattrstock = this.data.goodsinfo.sh_childs[k].sh_stock;
            this.data.selectattrmoney = this.data.goodsinfo.sh_childs[k].sh_show_price;
            this.data.goodsinfo.selectattrcommission = this.data.goodsinfo.sh_childs[k].sh_partner_commission;
          }
          this.data.goodsinfo.selectattrallid = this.data.goodsinfo.sh_childs[k].sh_id
          this.setData({
            priceflag: false
          })
        }
      }
      if (this.data.goodsinfo.selectattrstock > 0) {
        if (this.data.selectnum > this.data.goodsinfo.selectattrstock) {
          this.setData({
            selectnum: 1
          })
        }
        this.setData({
          minutesimg: '',
          plusimg: '3'
        })
      }
    }
    this.setData({
      selectattrmoney: this.data.selectattrmoney,
      goodsinfo: this.data.goodsinfo
    })
    // console.log(this.data.goodsinfo)
  },
  checkselectsku: function() {
    var that = this
    var bigarr = that.data.goodsinfo.sh_attributes
    if (bigarr.length > 1) {
      bigarr[0].sh_options.forEach(function(el, index) {
        if (el.active == true) { //第一个sku被选中,检查其他sku选中状态
          bigarr[1].sh_options.forEach(function(ele, index) {
            var codeago = el.sh_option_id + '_' + ele.sh_option_id
            that.data.goodsinfo.sh_childs.forEach(function(ela, index) {
              var codelast = ela.sh_sku_code;
              if (codeago == codelast) {
                if (that.data.buymode == "originbuy") {
                  ele.num = ela.sh_no_promotion_stock
                  if (ela.sh_no_promotion_stock == 0 || ela.sh_no_promotion_stock == null) {
                    ele.num = 0
                    ele.active = false
                  }
                  if (ele.active) {
                    that.data.selectattrmoney = ela.sh_no_promotion_show_price
                    that.data.goodsinfo.selectattrstock = ela.sh_no_promotion_stock
                    that.data.goodsinfo.selectattrcommission = ela.sh_no_promotion_partner_commission
                  } else {
                    that.data.selectattrmoney = that.data.goodsinfo.sh_no_promotion_show_price
                    that.data.goodsinfo.selectattrstock = that.data.goodsinfo.sh_no_promotion_all_stock
                    that.data.goodsinfo.selectattrcommission = that.data.goodsinfo.sh_max_no_promotion_partner_commission
                  }
                } else {
                  ele.num = ela.sh_stock;
                  if (ela.sh_stock == 0 || ela.sh_stock == null) {
                    ele.num = 0
                    ele.active = false
                  }
                  if (ele.active) {
                    that.data.selectattrmoney = ela.sh_show_price
                    that.data.goodsinfo.selectattrstock = ela.sh_stock
                    that.data.goodsinfo.selectattrcommission = ela.sh_partner_commission
                  } else {
                    that.data.selectattrmoney = that.data.goodsinfo.sh_no_promotion_show_price
                    that.data.goodsinfo.selectattrstock = that.data.goodsinfo.sh_no_promotion_all_stock
                    that.data.goodsinfo.selectattrcommission = that.data.goodsinfo.sh_max_no_promotion_partner_commission
                  }
                }
              }
            })
          })
        }
      })
      this.setData({
        selectattrmoney: this.data.selectattrmoney,
        goodsinfo: this.data.goodsinfo
      })
      bigarr[1].sh_options.forEach(function(el, index) {
        if (el.active == true) { //第二个sku被选中,检查其他sku选中状态
          bigarr[0].sh_options.forEach(function(ele, index) {
            var codeago = ele.sh_option_id + '_' + el.sh_option_id
            that.data.goodsinfo.sh_childs.forEach(function(ela, index) {
              var codelast = ela.sh_sku_code;
              if (codeago == codelast) {
                if (that.data.buymode == "originbuy") {
                  ele.num = ela.sh_no_promotion_stock;
                  if (ela.sh_no_promotion_stock == 0 || ela.sh_no_promotion_stock == null) {
                    ele.num = 0
                    ele.active = false
                  }
                  if (ele.active) {
                    that.data.selectattrmoney = ela.sh_no_promotion_show_price
                    that.data.goodsinfo.selectattrstock = ela.sh_no_promotion_stock
                    that.data.goodsinfo.selectattrcommission = ela.sh_no_promotion_partner_commission
                  }
                } else {
                  ele.num = ela.sh_stock;
                  if (ela.sh_stock == 0 || ela.sh_stock == null) {
                    ele.num = 0
                    ele.active = false
                  }
                  if (ele.active) {
                    that.data.selectattrmoney = ela.sh_show_price
                    that.data.goodsinfo.selectattrstock = ela.sh_stock
                    that.data.goodsinfo.selectattrcommission = ela.sh_partner_commission
                  }
                }
              }
            })
          })
        }
      })
      if (!bigarr[0].selectattr && !bigarr[0].selectattr) {
        if (that.data.buymode == "originbuy") {
          that.data.selectattrmoney = that.data.goodsinfo.sh_no_promotion_show_price
          that.data.goodsinfo.selectattrstock = that.data.goodsinfo.sh_no_promotion_all_stock
          that.data.goodsinfo.selectattrcommission = that.data.goodsinfo.sh_max_no_promotion_partner_commission
        } else {
          that.data.selectattrmoney = that.data.goodsinfo.sh_show_price
          that.data.goodsinfo.selectattrstock = that.data.goodsinfo.sh_all_stock
          that.data.goodsinfo.selectattrcommission = that.data.goodsinfo.sh_max_partner_commission
        }
      }
      this.setData({
        selectattrmoney: this.data.selectattrmoney,
        goodsinfo: this.data.goodsinfo
      })
    } else if (bigarr.length == 1) {

    }
  },
  setdisabled: function() {
    var bigarr = this.data.goodsinfo.sh_attributes
    var nowarr = []
    if (bigarr.length == 1) {
      for (var i = 0; i < bigarr.length; i++) {
        for (var j = 0; j < bigarr[i].sh_options.length; j++) {
          num = 0
          for (var k = 0; k < this.data.goodsinfo.sh_childs.length; k++) {
            var code = this.data.goodsinfo.sh_childs[k].sh_sku_code
            if (code == bigarr[i].sh_options[j].sh_option_id) {
              num = num + this.data.goodsinfo.sh_childs[k].sh_stock
            }
          }
          bigarr[i].sh_options[j].skustock = num
          if (num > 0) {
            nowarr.push(bigarr[i].sh_options[j])
          }
        }
      }
      if (nowarr.length > 0) {
        if (nowarr[0].skustock > 0) {
          nowarr[0].active = true
          bigarr[0].selectattrid = nowarr[0].sh_option_id
          bigarr[0].selectattr = nowarr[0].sh_alias_name
          for (var l = 0; l < this.data.goodsinfo.sh_childs.length; l++) {
            var code = this.data.goodsinfo.sh_childs[l].sh_sku_code
            if (code == nowarr[0].sh_option_id) {
              this.data.selectattrmoney = this.data.goodsinfo.sh_childs[l].sh_show_price
              this.data.goodsinfo.selectattrallid = this.data.goodsinfo.sh_childs[l].sh_id
              this.data.goodsinfo.selectattrstock = this.data.goodsinfo.sh_childs[l].sh_stock;
              this.data.goodsinfo.selectattrcommission = this.data.goodsinfo.sh_childs[l].sh_partner_commission;
            }
          }
          this.setData({
            thumbnail: nowarr[0].sh_image || '',
            goodsinfo: this.data.goodsinfo,
            selectattrmoney: this.data.selectattrmoney
          })
        }
      }
      this.setData({
        priceflag: false
      })
      console.log(this.data.goodsinfo)
    } else if (bigarr.length > 1) {
      for (var i = 0; i < bigarr.length; i++) {
        for (var j = 0; j < bigarr[i].sh_options.length; j++) {
          num = 0
          for (var k = 0; k < this.data.goodsinfo.sh_childs.length; k++) {
            var code = this.data.goodsinfo.sh_childs[k].sh_sku_code.split('_')
            if (code[0] == bigarr[i].sh_options[j].sh_option_id || code[1] == bigarr[i].sh_options[j].sh_option_id) {
              num = num + this.data.goodsinfo.sh_childs[k].sh_stock
            }
          }
          bigarr[i].sh_options[j].skustock = num
        }
      }
    }
    if (this.data.goodsinfo.sh_all_stock == 0) {
      this.setData({
        plusimg: '',
        inhibitInput: 'disabled'
      })
    }
    this.setData({
      goodsinfo: this.data.goodsinfo
    })
    // console.log(this.data.goodsinfo)
  },
  backhome: function() {
    wx.switchTab({
      url: '/pages/home/home'
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.teamdetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {
    clearInterval(goodstimer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    clearInterval(goodstimer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.teamdetail()
  }
})